package cn.growthgate.fgo.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import cn.growthgate.fgo.common.base.BaseServiceImpl;
import cn.growthgate.fgo.entity.ServantBond;
import cn.growthgate.fgo.mapper.ServantBondMapper;
import cn.growthgate.fgo.service.IServantBondService;

/**
 * <p>
 * 从者羁绊 服务实现类
 * </p>
 *
 * @author GrowthGate
 * @since 2019-05-28
 */
@Service
public class ServantBondServiceImpl extends BaseServiceImpl<ServantBondMapper, ServantBond>
		implements IServantBondService {

	@Override
	public boolean insertOrUpdate(ServantBond entity) {
		if (entity != null && entity.getId() == null) {
			// 检查重复数据，如果有则覆盖
			ServantBond servantBond = selectOne(
					newWrapper().eq("servantId", entity.getServantId()).eq("level", entity.getLevel()));
			if (servantBond != null) {
				entity.setId(servantBond.getId());
			}
		}
		return super.insertOrUpdate(entity);
	}

	@Override
	public List<ServantBond> selectByServantId(Long servantId) {
		return selectList(newWrapper().eq("servantId", servantId).orderBy("level"));	
	}

	@Override
	public int getLevelByValue(Long servantId, Integer value) {
		ServantBond servantBond = selectOne(newWrapper().eq("servantId", servantId).le("value", value).orderBy("level", false));	
		return servantBond != null ? servantBond.getLevel() : 0;
	}

}
